//
//  SingerViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SingerViewController.h"
#import "DanquViewController.h"

#import "MVViewController.h"


#import "SupdetailModel.h"


#import "SingerTableViewCell.h"


static CGFloat const titleH = 44;
//static CGFloat const navBarH = 64;
static CGFloat const maxTitleScale = 1.3;



#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
@interface SingerViewController ()<UIScrollViewDelegate>
@property (nonatomic , strong)  SupdetailModel *singer;

@property (nonatomic , strong)  UIScrollView *contentScrollView;
@property (nonatomic , strong)  UIScrollView *titleScrollView;
@property (nonatomic , strong)  NSMutableArray *buttons;
@property (nonatomic , strong)  UIButton *selTitleButton;

@end

@implementation SingerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBarHidden = NO;
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
//    self.view.backgroundColor = [UIColor whiteColor];
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.automaticallyAdjustsScrollViewInsets = NO;
   //数组初始化
    self.buttons = [NSMutableArray array];
    
//    NSLog(@"%@", self.singerId);
    
    [self setupTitleScrollView];
    
    [self createContentScrollView];
    [self addChildViewController];
    
    [self setupTitle];
    
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count *ScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.delegate = self;
    self.contentScrollView.bounces = NO;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;

//    [self dataHandle];
    
    
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.clipsToBounds = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:(UIBarMetricsDefault)];
    self.navigationController.navigationBar.clipsToBounds = NO;
}
// 设置头部标题
- (void)setupTitleScrollView
{
    // 判断是否有导航
//    CGFloat y = self.navigationController ? navBarH : 0;
    CGRect rect = CGRectMake(0, 0, ScreenW, titleH );
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
    titleScrollView.backgroundColor = [UIColor blackColor];
    titleScrollView.alpha = 0.4;
    titleScrollView.tintColor = [UIColor whiteColor];
//    titleScrollView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    [self.view addSubview:titleScrollView];
    self.titleScrollView = titleScrollView;
}

// 创建主要的滚动视图
- (void)createContentScrollView
{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y , ScreenW, ScreenH);
    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
    [self.view addSubview:contentScrollView];
    
    self.contentScrollView = contentScrollView;
    
    
}
// 添加子视图
- (void)addChildViewController
{
    DanquViewController *danquVC = [[DanquViewController alloc] init];
    danquVC.mId = self.singerId;
    danquVC.title = @"单曲";
    [self addChildViewController:danquVC];


    MVViewController *mvVC = [[MVViewController alloc] init];
    mvVC.mId = self.singerId;
    mvVC.title = @"MV";
    [self addChildViewController:mvVC];



}

// 设置标题
- (void)setupTitle
{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = WIDTH / 2;
    CGFloat h = titleH;
    
    for (int i = 0; i < count; i++) {
        UIViewController *VC = self.childViewControllers[i];
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        UIButton *button = [[UIButton alloc] initWithFrame:rect];
        button.tag = i;
        [button setTitle:VC.title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [button addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        [self.buttons addObject:button];
        [self.titleScrollView addSubview:button];
        
        if (i == 0) {
            [self chick:button];
        }
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}
// 按钮点击
- (void)chick:(UIButton *)button
{
    [self selTitleButton:button];
    NSUInteger i = button.tag;
    CGFloat x = i * ScreenW;
    [self setUpOneChildViewController:i];
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
}

// 选中按钮
- (void)selTitleButton:(UIButton *)button
{
//    self.selTitleButton.selected = NO;
    [self.selTitleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.selTitleButton.transform = CGAffineTransformIdentity;
    // 设置当前选中的按钮
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
   
    self.selTitleButton = button;
    // 设置选中按钮保持在中心点附近位置
    [self setupTitleCenter:button];
    
}
- (void)setupTitleCenter:(UIButton *)button
{
    CGFloat offset = button.center.x - ScreenW * 0.5;
    // 如果偏移量小于0,说明按钮的中心点是在屏幕中心点的左侧,所以scrollview就不用偏移,保持偏移量为0就行.
    // 如果按钮中心点大于屏幕中心点,那么scrollview就偏移二者的差距就行.

    if (offset < 0) {
        offset = 0;
    }
    // scrollview的内容宽度-屏幕宽度,就是最大偏移量.也就是scrollview偏移最大偏移量就能显示最后的按钮了.
    CGFloat maxOffset = self.titleScrollView.contentSize.width - ScreenW;
    if (offset > maxOffset) {
        offset = maxOffset;
    }
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
}




- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * WIDTH;
    UIViewController *VC = self.childViewControllers[i];
    if (VC.view.superview) {
        return;
    }
    VC.view.frame = CGRectMake(x, 0, ScreenW, ScreenH - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:VC.view];
}





- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = self.contentScrollView.contentOffset.x / ScreenW;
    [self selTitleButton:self.buttons[i]];
    [self setUpOneChildViewController:i];
    
    
}



// 只要滚动就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetx = scrollView.contentOffset.x;
    // 左边按钮就是当前选中的按钮
    NSInteger leftIndex = offsetx / ScreenW;
    NSInteger rightIndex = leftIndex + 1;
    UIButton *leftButton = self.buttons[leftIndex];
    UIButton *rightBUtton = nil;
    if (rightIndex < self.buttons.count) {
        rightBUtton = self.buttons[rightIndex];
    }
    CGFloat scaleR = offsetx / ScreenW - leftIndex;
    CGFloat scaleL = 1 - scaleR;
    
    // 设置所有按钮的缩放范围都是1 - 1.3
    CGFloat transaScale = maxTitleScale - 1;
    // 0 -1 范围的数*0.3 永远是0 - 0.3的范围
    leftButton.transform = CGAffineTransformMakeScale(scaleL * transaScale + 1, scaleL * transaScale + 1);
    rightBUtton.transform = CGAffineTransformMakeScale(scaleR * transaScale + 1, scaleR * transaScale + 1);
    
    UIColor *rightColor = [UIColor colorWithRed:scaleR green:0 blue:0 alpha:1];
    UIColor *leftColor = [UIColor colorWithRed:scaleL green:0 blue:0 alpha:1];
    
    [leftButton setTitleColor:leftColor forState:UIControlStateNormal];
    [rightBUtton setTitleColor:rightColor forState:UIControlStateNormal];
    
    
}




@end
